package com.cms.admin.action.order;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.*;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.order.ImMessageService;
import com.cms.admin.service.order.RegisterOrderService;
import com.cms.admin.util.Constants;
import com.cms.admin.util.PropertiesService;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/17.
 */
public class ImMessageAction extends BaseAction implements ModelDriven<IMsgIMMessageExt>{

    private RegisterOrderService registerOrderService;
    private ImMessageService imMessageService;
    private TemplateService templateService;

    private IMsgIMMessageExt search=new IMsgIMMessageExt();

    public String aiaxQueryOrderImMessage()
    {
        RegisterOrderExt registerOrder=registerOrderService.queryOrderDetailWithUrl(search.getGroupid());
        if (null!=registerOrder)
        {
            List<IMsgIMMessage> iMsgIMMessages=imMessageService.queryIMsgIMMessagesByGroupid(search.getGroupid());
            String res = null;
            try {
                res=getQueryResult(iMsgIMMessages,registerOrder);

                JSONObject o = new JSONObject();
                o.put("content", res);
                o.put("errorCode",0);
                o.put("errorMsg","success");

                return ajaxJson(o);
            } catch (Exception e) {
                e.printStackTrace();
                JSONObject o = new JSONObject();
                o.put("errorCode",-1);
                o.put("errorMsg","查询聊天信息失败");
                return ajaxJson(o);
            }

        }else
        {
            JSONObject o = new JSONObject();
            o.put("errorCode",-1);
            o.put("errorMsg","参数错误，查询不到相关订单信息");
            return ajaxJson(o);
        }
    }

    private String getQueryResult(List<IMsgIMMessage> iMsgIMMessages, RegisterOrder order)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("messageList", iMsgIMMessages);
        map.put("order", order);
        map.put("resPath", PropertiesService.getString("res.path"));
        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_IMMESSAGE_QUERY_RESULT, map);
        return result;
    }
    @Override
    public IMsgIMMessageExt getModel() {
        return search;
    }

    public RegisterOrderService getRegisterOrderService() {
        return registerOrderService;
    }

    public void setRegisterOrderService(RegisterOrderService registerOrderService) {
        this.registerOrderService = registerOrderService;
    }

    public ImMessageService getImMessageService() {
        return imMessageService;
    }

    public void setImMessageService(ImMessageService imMessageService) {
        this.imMessageService = imMessageService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }
}
